


;==========================================
;=                                        =
;= COPY, MIRROR, SHIFT 2px, 4px, 6px =
;=                                        =
;==========================================
;
;copysprite
;	HL = source
;	B = lines
;	C = width
;copymask
;	HL = source
;	B = lines
;	C = width
;
;mirrorsprite
;	HL = source
;	B = lines
;	C = width
;
;mirrormask
;	HL = source
;	B = lines
;	C = width
;
;shift
;	HL = buffer to shift
;	C = bytes / 8
;	A = pixels to shift
;
;



	.MODULE copyshift

copysprite
	;	HL = source
	;	B = lines
	;	C = width
	ld de, GLOB_SPRBUF
	xor a
	jr _copy2 
copymask
	ld de, GLOB_MSKBUF
	ld a, 255
_copy2	
	push de
	ld e, a
	ld a, 2		; + 4B: ld (de), a
			;       inc e
			;       djnz x 
	add a, c
	add a, a	; 
	neg		; -(width*2 + 4)
	ld (_copy1 + 1), a
	
	ld a, e
	pop de
	ld (de), a
	inc e
	inc b		;because: while ((B--)!=0) { copy_line }
	ld c, 255
	jr _copy1

	ldi
	ldi
	ldi
	ldi
	
	ldi
	ldi
	ldi
	ldi		;copy byte

	ld (de), a	;insert pad byte $00(pixels)/$FF(mask) for shift
	inc e
_copy1
	djnz 0	
	ret

mirrorsprite
	;	HL = source
	;	B = lines
	;	C = width
	;
	;        SRC0  SRC1   SRCX-1  SRCX
	;   PAD  BUFX  BUFX-1 BUF1    BUF0  PAD
	;
	ld d, GLOB_SPRBUF/256
	xor a
	jr _mirror2
mirrormask
	ld d, GLOB_MSKBUF/256 
	ld a, 255
_mirror2
	ld (_mirror1 + 1), a	;store pad byte
	
	ld e, c		; start at address buffer+width and go down 
			; put width*mirrored_source + 1 pad
	
	ld a, c
	add a, a
	inc a
	ld (_mirror4 + 1), a	;offset to next line (width*2 + 1)

	ld a, c
	add a, a
	add a, a
	add a, c		;mirror snippet takes 5B
	add a, 11		;pad byte and loop control
	neg
	ld (_mirror3 + 1), a	


	ld lx, b

	neg
		
	ld b, GLOB_MIRRTAB / 256
	jp _mirror3
	

	ld c, (hl)	;1
	inc l		;1
	ld a, (bc)	;1
	ld (de), a	;1
	dec e		;1 5B

	ld c, (hl)
	inc l
	ld a, (bc)
	ld (de), a
	dec e
	
	ld c, (hl)
	inc l
	ld a, (bc)
	ld (de), a
	dec e

	ld c, (hl)
	inc l
	ld a, (bc)
	ld (de), a
	dec e

	ld c, (hl)
	inc l
	ld a, (bc)
	ld (de), a
	dec e

	ld c, (hl)
	inc l
	ld a, (bc)
	ld (de), a
	dec e

	ld c, (hl)
	inc l
	ld a, (bc)
	ld (de), a
	dec e

	ld c, (hl)
	inc l
	ld a, (bc)
	ld (de), a
	dec e
_mirror1
	ld a, 0 	;2
	ld (de), a	;1
_mirror4
	ld a, 0		;2
	add a, e	;1
	ld e, a		;1

	dec lx		;2
_mirror3
	jr nz, 0	;JP would be faster but it takes more time to compute address and its not worth for smal values like 8-24 lines 
	ret


shift
	;HL = pixel buffer / mask buffer
	;C = bytes / 8 
	;A = shift in pixels

	
	or a
	jr nz, _shift1
	inc hl
	ret
_shift1
	rra
	dec a
	jr z, _shift2
	dec a
	jr z, _shift4
 	;shift by 6
	;means shift by 2 to left
	ld a, c
	add a, a
	add a, a
	add a, a
	ld l, a
	ld a, (hl)	;the very last byte, either 0x00 or 0xff
	dec l
	jp _shift_6

_shift_2	
	ld a, (hl)
	inc l
	push hl
	call _shift_22	;2px
	pop hl
_shift_22
	rra
	ld b, c 
_shift_21
	rr (hl)
	inc l
	rr (hl)
	inc l
	rr (hl)
	inc l
	rr (hl)
	inc l

	rr (hl)
	inc l
	rr (hl)
	inc l
	rr (hl)
	inc l
	rr (hl)
	inc l
	djnz _shift_21
	ld l, 1
	ret


_shift_4
	ld a, (hl)
	inc l
	ld b, c
_shift_41
	rrd
	inc l
	rrd
	inc l
	rrd
	inc l
	rrd 
	inc l
	
	rrd
	inc l
	rrd
	inc l
	rrd
	inc l
	rrd 
	inc l
	djnz _shift_41
	ld l, 1
	ret


_shift_6
	push hl			;two times left
	call _shift_62
	pop hl
_shift_62
	ld b, c
	rla
_shift_61
	rl (hl)
	dec l
	rl (hl)
	dec l
	rl (hl)
	dec l
	rl (hl)
	dec l	

	rl (hl)
	dec l
	rl (hl)
	dec l
	rl (hl)
	dec l
	rl (hl)
	dec l	
	djnz _shift_61
	ld l, b			
	ret			